//
//  MoreViewController.m
//  DataStatistics
//
//  Created by Kang on 15/8/20.
//  Copyright (c) 2015年 YTYangK. All rights reserved.
//

#import "MoreViewController.h"

#import "Webview.h"
#import "isPhoneNumber.h"
#import "LoginVC.h"
#import "MBProgressHUD.h"
#import "JparkViewController.h"
#import "UserViewModel.h"
#import "AppDelegate.h"
#import "GUAAlertView.h"
#import "JPUSH.h"
#import "ChangePasswordViewController.h"

@interface MoreViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>//UIViewControllerRestoration
{
    float W_and_H;
    BOOL _isLarge;
    NSInteger hour;
    NSInteger minute;
    CAKeyframeAnimation *_animation;
    // 控制按钮触发事件顺序
    BOOL isShowWview;
    
}

//页面布局全局变量
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *privacyAndPasswordBtm;// 修改密码按钮
@property (strong,nonatomic) NSMutableDictionary *parameter;
@property (weak, nonatomic) IBOutlet UIButton *SetUp;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *service;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *password;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *PandC;
//@property (strong, nonatomic)
@end

@implementation MoreViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.restorationIdentifier = @"MDC_More";
    self.restorationClass = [self class];


    [self updateUI];
    [self NSLayoutConstraint];
    
    
    _animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];

    

    


}

- (NSString *)getMoneyStringWithMoneyNumber:(double)money{
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    // 设置格式
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
    NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:money]];
    return formattedNumberString;
}

- (void) viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    [self.BalanceLabel.layer removeAnimationForKey:@"shake"];//动画停止

}

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [[JPUSH sharedJPUSH] albbBindChannel];

    
    CGRect viewBounds = self.view.bounds;
    CGFloat topBarOffset = -20.0;
    viewBounds.origin.y = -topBarOffset;
    self.view.bounds = viewBounds;
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self.navigationController setNavigationBarHidden:NO];
    
    
    
    self.BalanceLabel.text = [NSString stringWithFormat:@"余额：￥%@",    [self getMoneyStringWithMoneyNumber:[[NSUD objectForKey:@"money"] doubleValue]]];
    _animation.values = @[@1.0,@1.3,@0.9,@1.15,@0.95,@1.02,@1.0];
    _animation.duration = 2.0;
    _animation.calculationMode = kCAAnimationCubic;
    _animation.repeatCount = MAXFLOAT;
    
    [[self.BalanceLabel layer] addAnimation:_animation forKey:@"shake"];


    
    
}


- (void)updateViewConstraints {
    [super updateViewConstraints];
    if (SCREEN_HEIGHT==480) {
    }
}



#pragma mark - 设置文字 
- (void)updateUI {
    isShowWview = NO;
    self.view.backgroundColor = RGB(33, 38, 44, 1);
    self.title = NSLocalizedString(@"CpvcLead_title", nil);
    
    [self.SetUp setTitle:NSLocalizedString(@"Contact Customer Service", @"联系客服") forState:UIControlStateNormal];

    UIButton * btn =  [self.privacyAndPasswordBtm objectAtIndex:1];
    [btn setTitle:NSLocalizedString(@"CpvcLead_pas", @"修改密码") forState:UIControlStateNormal];
    
    
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon-exit"] style:UIBarButtonItemStyleDone target:self action:@selector(tuichu)];
    self.navigationItem.rightBarButtonItem = leftBtn;

}







//修改密码跳转
- (IBAction)gotoChangePas:(id)sender {
    
    

//    [MBProgressHUD showError:@"尚未开通，敬请期待！" toView:self.view];
    
//    ChangePasswordViewController *password = [[ChangePasswordViewController alloc]init];
//    [self.navigationController pushViewController:password animated:YES];
//
//
}



//拨打客服电话
- (IBAction)phone:(id)sender {
    if (isShowWview == NO) {
        
            UIWebView *callWebView = [[UIWebView alloc] init];
            
            NSURL *telURL = [NSURL URLWithString:@"tel:400-662-6806"];
            
            [callWebView loadRequest:[NSURLRequest requestWithURL:telURL]];
            
            [self.view addSubview:callWebView];
    }
}





/* 退出登录 */
- (void)tuichu{
    
    self.navigationItem.rightBarButtonItem.enabled = NO;

    GUAAlertView *v = [GUAAlertView alertViewWithTitle:@"提示"
                                        message:NSLocalizedString(@"CpvcLead_2", @"退出提示")
                                           buttonTitle:NSLocalizedString(@"Log out", nil)
                                   buttonTouchedAction:^{
                                       NSLog(@"button touched");
                                       
                                       [MoreViewController logout:self];
                                       
                                   } dismissAction:^{
                                       NSLog(@"dismiss");
                                       self.navigationItem.rightBarButtonItem.enabled = YES;
      
                                   }];
    [v show];

}


/* 退出登录 */
+ (void)logout:(UIViewController *)VC {
    [MoreViewController logoutOnline:VC];
}

+ (void)moreAbnormalExit:(UIViewController *)vc {
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    [MoreViewController removeMDCData];             /// 删除绑定的全部标签
    UIStoryboard * storyboard =[UIStoryboard storyboardWithName:@"Login" bundle:NULL];
    LoginVC *log              = [storyboard instantiateInitialViewController];
    UIWindow *window          = [UIApplication sharedApplication].keyWindow;
    [vc presentViewController:log animated:YES completion:^{
        window.rootViewController = log;
        [JPUSH sharedJPUSH].sonWindow = nil;
        [JPUSH sharedJPUSH].tabBarC = nil;
        [[JPUSH sharedJPUSH] Deleteaccount];
    }];
}



+ (void)logoutOnline:(UIViewController *)vc {
    /**
     *  退出登录请求方法
     */
    [UserViewModel requestWithUrlForlogoutOnline:self andParam:@{@"token":[NSUD objectForKey:@"token"]} success:^(id model) {
        [MoreViewController moreAbnormalExit:vc];
    } failure:^(NSString *err) {
        [MBProgressHUD yty_showErrorWithTitle:nil detailsText:err toView:nil];
    }];
}


+ (void)removeMDCData{
    
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    [NSUD removeObjectForKey:@"token"];
    [NSUD removeObjectForKey:MDC_USER_INFO_DIC];
    [NSUD removeObjectForKey:MDC_PASSWORD_VLUE];
    [NSNC removeObserver:BADGE_Home];
    [NSNC removeObserver:BADGE_Num];
    [NSNC removeObserver:UIApplicationDidBecomeActiveNotification];
    [NSNC removeObserver:MDC_SET_BADGE];
    [NSNC removeObserver:MDC_NOTIFICATION_NICK];
    [NSNC removeObserver:MDC_USERCODE];
    

    [[UIApplication sharedApplication] registerForRemoteNotifications]; // 清除全部通知。
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; //角标清0

}


- (void)NSLayoutConstraint
{
    
    if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"480.0")) {

        self.TitleLayout.constant = 70;
        self.NSlayout2.constant = 20;

    
    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"568.0")) {
        self.NSlayout2.constant = 25;
        self.password.constant = 43;
        self.service.constant = 43;
        self.PandC.constant = 8;
        self.TitleLayout.constant = 90;
        
        self.SetUp.imageEdgeInsets = UIEdgeInsetsMake(2,-2,2,2);
        UIButton * btn =  [self.privacyAndPasswordBtm objectAtIndex:1];
        btn.imageEdgeInsets =UIEdgeInsetsMake(2,-2,2,2);
        
    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"667.0")){
        
        self.NSlayout2.constant = 40;
        
    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"736.0")){
        self.TitleLayout.constant = 130;
        self.NSlayout2.constant = 60;
        self.password.constant = 53;
        self.service.constant = 53;
        self.PandC.constant = 15;
    }
}

@end
